import { Options } from "amqplib";
import { QueueHandler } from "./types";
import dayjs from 'dayjs'
import { listenDelayTask, publishToDelayTask } from "./delayTask";

function getDelay() {
    return Math.floor((dayjs().endOf('day').millisecond() - dayjs().millisecond()) / 1000)
}

export async function listenDailyTask(type: string, handler: QueueHandler) {
    await listenDelayTask(type, async (msg, c) => {
        await handler(msg, c)
        publishToDailyTask(type, msg.content)
    })
}

export async function publishToDailyTask(type: string, msg: Buffer, options: Options.Publish = {}) {
    await publishToDelayTask(type, msg, getDelay(), options)
}
